package cn.wit.summit.chwan.adapters;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.PicCollectionTypeListResponse;

/**
 * Created by Alan on 2018/4/4.
 */

public class ImageListTopBarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Context mContext;
    private LayoutInflater inflater;
    private int selectId = 0;
    private OnChangeItem mOnChangeItem;

    public ImageListTopBarAdapter(Context context, List<ViewBean> datas, int position, OnChangeItem mOnChangeItem) {
        this.mContext = context;
        this.mDatas = datas;
        this.mOnChangeItem = mOnChangeItem;
        inflater = LayoutInflater.from(mContext);

        setSelectId(position);
    }

    public String setSelectId(int selectId) {
        this.selectId = selectId;
        notifyDataSetChanged();
        if (selectId >= mDatas.size()) {
            selectId = mDatas.size() - 1;
        }
        return ((PicCollectionTypeListResponse.PicListBean)mDatas.get(selectId).getObj()).getId();
    }

    public enum ViewType {
        TYPE_BASE,
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public static class TitleBean {
            public String time;

            public TitleBean(String time) {
                this.time = time;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_BASE.ordinal()) {
            holder = new BaseHolder(inflater.inflate(R.layout.item_imagetop_bar_view, parent, false));
        }

        return holder;
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof BaseHolder) {
            PicCollectionTypeListResponse.PicListBean temp = (PicCollectionTypeListResponse.PicListBean) viewBean.getObj();
            BaseHolder baseHolder = (BaseHolder) holder;

            baseHolder.itemBarTv.setText(temp.getTitle());
            baseHolder.itemBarNumTv.setText(temp.getPicCount()+"P");
            if (selectId == position) {
                baseHolder.itemBarRootLl.setBackgroundResource(R.drawable.image_bar_select_bg);
                baseHolder.itemBarTv.setTextColor(mContext.getResources().getColor(R.color.color_white));
                baseHolder.itemBarNumTv.setTextColor(mContext.getResources().getColor(R.color.color_white));
            } else {
                baseHolder.itemBarRootLl.setBackgroundResource(R.drawable.image_bar_unselect_bg);
                baseHolder.itemBarTv.setTextColor(mContext.getResources().getColor(R.color.color_848484));
                baseHolder.itemBarNumTv.setTextColor(mContext.getResources().getColor(R.color.color_848484));
            }

            baseHolder.itemBarRootLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnChangeItem.onchange(position);
                }
            });
        }
    }

    private String getChangeTx(String title, String s) {
        String str = "";
        SpannableString spannableString = new SpannableString(s);
        spannableString.setSpan(new AbsoluteSizeSpan(11, true), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new StyleSpan(Typeface.NORMAL), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        str = title + " " + spannableString;
        return str;
    }

    class BaseHolder extends RecyclerView.ViewHolder {
        LinearLayout itemBarRootLl;
        TextView itemBarNumTv;
        TextView itemBarTv;

        public BaseHolder(View view) {
            super(view);
            itemBarTv = view.findViewById(R.id.itemBarTv);
            itemBarNumTv = view.findViewById(R.id.itemBarNumTv);
            itemBarRootLl = view.findViewById(R.id.itemBarRootLl);
        }
    }

    public interface OnChangeItem {
        void onchange(int position);
    }
}
